package org.wattdepot.cli.command;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Tests the TotalCommand command handler.
 * @author George Lee
 *
 */
public class TestTotalCommand extends CommandTestBed {

  /**
   * Tests the doCommand method of the TotalCommand class.
   */
  @Override
  @Test
  public void testDoCommand() {
    String command, result;
    TotalCommand listTotal = new TotalCommand(this.client);

    // Sad tests
    command = "total";
    result = listTotal.doCommand(command);
    assertFalse("Checking that the list total command works.", 
        result.startsWith(WattDepotCliCommand.UNKNOWN_COMMAND));
    assertTrue("Checking that there are not enough arguments to list total.",
        result.contains(WattDepotCliCommand.PARAMETER_COUNT_ERROR));

    command = "total foo generated SIM_OAHU_GRID day 2009-11-01 sampling-interval 60";
    result = listTotal.doCommand(command);
    assertTrue("Checking that an invalid parameter is passed.", 
        result.contains("Invalid type"));

    command = "total carbon bar SIM_OAHU_GRID day 2009-11-01 sampling-interval 60";
    result = listTotal.doCommand(command);
    assertTrue("Checking that an invalid parameter is passed.", 
        result.contains(WattDepotCliCommand.PARAMETER_ERROR));

    command = "total carbon generated SIM_OAHU_GRID day foo sampling-interval 60";
    result = listTotal.doCommand(command);
    assertTrue("Checking that the timestamp result is invalid.", result
        .startsWith(WattDepotCliCommand.TSTAMP_ERROR));

    command = "total carbon generated SIM_OAHU_GRID day 2009-11-01 sampling-interval -1";
    result = listTotal.doCommand(command);
    assertTrue("Checking that the sampling interval is invalid.",
        result.contains(WattDepotCliCommand.RANGE_ERROR));

    // Happy tests
    command = "total carbon generated SIM_OAHU_GRID day 2009-11-01 sampling-interval 60";
    result = listTotal.doCommand(command);
    assertFalse("Checking that the correct type is passed.", result.startsWith("Invalid type"));
    assertFalse("Checking that the parameters are correct.",
        result.startsWith(WattDepotCliCommand.UNKNOWN_COMMAND));
    assertFalse("Checking that the timestamp result is correct.", result
        .startsWith(WattDepotCliCommand.TSTAMP_ERROR));
    assertFalse("Checking that the interval is correct.",
        result.startsWith(WattDepotCliCommand.INTERVAL_ERROR));
    
    command = "total energy generated SIM_OAHU_GRID day 2009-11-05 sampling-interval 120";
    result = listTotal.doCommand(command);
    assertFalse("Checking that the list command works.", "Unknown command".equals(result));
    assertFalse("Checking that there are enough arguments to the command.",
        "Not enough arguments to list command".equals(result));
    assertFalse("Checking that the correct type is passed.", result.startsWith("Invalid type"));
    assertFalse("Checking that the parameters are correct.",
        result.startsWith(WattDepotCliCommand.UNKNOWN_COMMAND));
    assertFalse("Checking that the timestamp result is correct.", result
        .startsWith("Could not create timestamp from"));
    assertFalse("Checking that the interval is correct.",
        result.startsWith(WattDepotCliCommand.INTERVAL_ERROR));
  }

}
